package com.cxs.service.impl;

import com.cxs.mapper.GoodDetailMapper;
import com.cxs.model.GoodDetail;
import com.cxs.model.GoodDetailExample;
import com.cxs.service.GoodDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/*
 * @Author:cxs
 * @Date:2021/10/2  19:23
 * @Version 1.0
 * @Motto:放下杂念,只为迎接明天更好的自己
 * */
@Service
public class GoodDetailServiceImpl implements GoodDetailService {

    @Autowired
    private GoodDetailMapper goodDetailMapper;

    @Override
    public List<GoodDetail> list(Integer cid) {
        return goodDetailMapper.selectAll(cid);
    }

    @Override
    public String sale(Integer id, Integer num) {
        String result = "购买失败";
        Assert.notNull(id,"id为空!");
        Assert.notNull(num,"数量为空!");

        GoodDetail origin = goodDetailMapper.selectByPrimaryKey(id);
        Integer remaining = origin.getRemaining();
        if(remaining != 0 && remaining >= num){
            GoodDetail goodDetail = new GoodDetail();
            goodDetail.setId(id);
            goodDetail.setRemaining(remaining - num);
            if(goodDetailMapper.updateByPrimaryKeySelective(goodDetail) > 0) result = "购买成功";
        }
        return result;
    }
}
